package com.example.shoppingmall.shoppingcart.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.shoppingmall.R;

/**
 *  自定义删除增加按钮
 * */
public class AddSubView  extends LinearLayout implements View.OnClickListener {
    private ImageView ivSub;
    private TextView tvValue;
    private ImageView ivAdd;
    private int value = 1;
    private int minValue = 1;
    private int maxValue = 5;

    /**
     * Find the Views in the layout<br />
     * <br />
     * Auto-created on 2022-01-22 13:55:16 by Android Layout Finder
     * (http://www.buzzingandroid.com/tools/android-layout-finder)
     */
    private void findViews() {
        ivSub = (ImageView)findViewById( R.id.iv_sub );
        tvValue = (TextView)findViewById( R.id.tv_value );
        ivAdd = (ImageView)findViewById( R.id.iv_add );
    }



    private final Context mContext;

    public AddSubView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        // 把布局文件实列化，并且加载到addSubView类中
        View.inflate(context, R.layout.add_sub_view,this);
        findViews();

        int value = getValue();
        setValue(value);

        // 设置点击事件
        ivSub.setOnClickListener(this);
        ivAdd.setOnClickListener(this);
    }




    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_sub: // 减
                subNumber();
                break;
            case R.id.iv_add: // 加
                addNumber();
                break;
        }

    }

    private void addNumber() {
        Log.e("Tag","触发了");
        if(value < maxValue){
            value ++;
        }
        setValue(value);
        if(onNumberChangeListener != null){
            onNumberChangeListener.onNumberChange(value);

        }

    }

    private void subNumber() {
        if(value > minValue){
            value --;
        }
        setValue(value);
        if(onNumberChangeListener !=null){
            onNumberChangeListener.onNumberChange(value);
        }
    }

    public int getValue() {
        String valueStr = tvValue.getText().toString().trim();
        if(!TextUtils.isEmpty(valueStr)){
            value = Integer.parseInt(valueStr);
        }
        return value;
    }
    public void setValue(int value) {
        this.value = value;
        tvValue.setText(value+"");
    }

    public int getMinValue() {
        return minValue;
    }

    public void setMinValue(int minValue) {
        this.minValue = minValue;
    }

    public int getMaxValue() {
        return maxValue;
    }

    public void setMaxValue(int maxValue) {
        this.maxValue = maxValue;
    }

    // 当数量发生变化的时候回调
    public interface onNumberChangeListener{
        // 当数据发生变化的时候回调
        public void onNumberChange(int value);
    }

    private onNumberChangeListener onNumberChangeListener;

    /**
     * 设置数量变化的监听
     * */
    public void setOnNumberChangeListener(onNumberChangeListener onNumberChangeListener){
        this.onNumberChangeListener = onNumberChangeListener;
    }
}
